//
//  CollectManager.m
//  FreeReading
//
//  Created by lanou3g on 15/11/7.
//  Copyright © 2015年 lanou3g. All rights reserved.
//

#import "CollectManager.h"

@interface CollectManager ()
@property (nonatomic, strong) FMDatabase *database;
@end

@implementation CollectManager


+ (CollectManager *)shareManager
{
    
    static CollectManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
       
        manager = [[[self class] alloc] init];
        
        
        
    });
    
    return manager;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self path];
    }
    return self;
}

- (void)path
{
    
    // 获取路径
    NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
    NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"FreeReading"];
    
    // 初始化FMDB
    self.database = [FMDatabase databaseWithPath:dbPath];
    
    [self creatTable];
    
}

// 创建表
- (void)creatTable
{
    [self.database open];
    
    [self.database executeUpdate:@"CREATE TABLE IF NOT EXISTS FREEREADING (title TEXT PRIMARY KEY not NULL, imageUrl TEXT, url TEXT not NULL, tag text not null)"];
    [self.database close];
}

// 插入
- (void)insertDatabaseWithTitle:(NSString *)title picUrl:(NSString *)picUrl webUrl:(NSString *)webUrl tag:(NSString *)tag
{
    [self.database open];
    if (picUrl == nil) {
        picUrl = @"";
    }
    FMResultSet *result = [self.database executeQuery:@"insert into FREEREADING(title, imageUrl, url, tag) values(?,?,?,?)" withArgumentsInArray:@[title, picUrl, webUrl, tag]];
    
    // 执行
    [result next];
    
    [self.database close];
}

// 删除
- (void)deleteDatabaseWithTitle:(NSString *)title
{
    [self.database open];
    
    [self.database executeUpdateWithFormat:@"delete from FREEREADING Where title = %@", title];
    
    [self.database close];
}

// 查询
- (NSArray *)selectDatabaseWithTitle:(NSString *)title
{
    NSMutableArray *dataArray = [NSMutableArray array];
    
    [self.database open];
    
    FMResultSet *result = [self.database executeQuery:@"select * from FREEREADING Where title = ?", title];
    
    while ([result next] == YES) {
        
        NSString *title = [result stringForColumn:@"title"];
        NSString *imageUrl = [result stringForColumn:@"imageUrl"];
        NSString *url = [result stringForColumn:@"url"];
        NSString *tag = [result stringForColumn:@"tag"];
        
        NSDictionary *dic = @{@"title":title, @"imageUrl":imageUrl, @"url":url, @"tag":tag};
        
        [dataArray addObject:dic];
    }
    
    [self.database close];
    
    return dataArray;
}

- (NSArray *)selectDatabaseWithTag:(NSString *)tag
{
    NSMutableArray *dataArray = [NSMutableArray array];
    
    [self.database open];
    
    FMResultSet *result = [self.database executeQuery:@"select * from FREEREADING Where tag = ?", tag];
    
    while ([result next] == YES) {
        
        NSString *title = [result stringForColumn:@"title"];
        NSString *imageUrl = [result stringForColumn:@"imageUrl"];
        NSString *url = [result stringForColumn:@"url"];
        NSString *tag = [result stringForColumn:@"tag"];
        
        NSDictionary *dic = @{@"title":title, @"imageUrl":imageUrl, @"url":url, @"tag":tag};
        
        [dataArray addObject:dic];
    }
    
    [self.database close];
    
    return dataArray;
}




@end
